﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

<% 
       List<string> hideFilters=new List<string>();

       if (ViewData["HideFilters"] != null)
       {
           hideFilters = ViewData["HideFilters"].ToString().Split(',').ToList();
       }
       
        Type type = Model.GetType();
        if (type.IsGenericType && type.GetGenericTypeDefinition()
                == typeof(List<>))
        {
            Type itemType = type.GetGenericArguments()[0]; // use this...

            ModelMetadata meta = ModelMetadataProviders.Current.GetMetadataForType(null, itemType);
            var filters = meta.Properties.Where(modelMetadata => !hideFilters.Contains(modelMetadata.PropertyName) && modelMetadata.AdditionalValues.Any(ad => ad.Key == "CascadingFilter"))
                .Select(metadata => new
                    {
                        PropertyName = metadata.AdditionalValues.ContainsKey("PropertyName") ? metadata.AdditionalValues["PropertyName"].ToString() : metadata.PropertyName,
                        Type = metadata.AdditionalValues["CascadingFilter"],
                        Order = metadata.AdditionalValues["CascadingFilterOrder"],
                        CascadingChain = metadata.AdditionalValues["CascadingChain"],
                        metadata.DisplayName
                    }).ToList().OrderBy(o => o.Order);

            
            foreach (var filter in filters)
            {
                if (filter.Type != null && filter.CascadingChain != null && filter.Type.ToString().Trim().ToLower() == "cascadingfilter")
                {
                    string[] cascadingChain = filter.CascadingChain.ToString().Split('$');
                    for (int i = cascadingChain.Length - 1; i >= 0; i--)
                    {
                        string[] items = cascadingChain[i].Split('!');
                        string chainPropertyName = items[2];
                        bool isFirstItem = i == (cascadingChain.Length - 1);
                        bool isLastItem = i == 0;

                        if (!isFirstItem)
                        {
                            string[] parentItems = cascadingChain[i + 1].Split('!');
                            string parentTableName = parentItems[1];
                            string parentPropertyName = parentItems[2];
                            if ((int)ViewData[parentPropertyName] != -1)
                            {
                                int parentID = (int)ViewData[parentPropertyName];
                                if ((int)ViewData[chainPropertyName] == -1)
                                    ViewData[chainPropertyName] = Smartec.Accounts.Camel.AMS.Model.Facades.AMSManagerFacade.GetFieldValue(parentTableName, chainPropertyName, parentID);
                            }
                        }
                    }
                }
            }
                
            foreach (var filter in filters)
            {
                if (filter.Type != null && filter.CascadingChain != null && filter.Type.ToString().Trim().ToLower() == "cascadingfilter")
                {
                    ViewDataDictionary VDD = new ViewDataDictionary();
                    VDD["EntityName"] = itemType.Name;
                    VDD["FieldName"] = filter.PropertyName;
                    VDD["Mode"] = "Filter";
                    foreach (KeyValuePair<string, object> kv in ViewData)
                    {
                        VDD.Add(kv);
                    }
                    %>
                    <tr><td><%: filter.DisplayName %></td><td><% Html.RenderPartial("~/Views/Shared/CascadingField.ascx", VDD);%></td></tr>
                    <%
                }
            }
        }
%>